package com.kuangstudy.controller;

import com.kuangstudy.constant.RedisConstants;
import com.kuangstudy.entity.Content;
import com.kuangstudy.utils.bean.BeanMapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author panghu
 * @version 1.0.0
 * @createTime 2023年09月16日 22:05:00
 * @Description
 */
@RestController
@Slf4j
@Api(tags = "发送微博")
public class SendWeiBoController {

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/send/content")
    @ApiOperation("发送内容")
    public Content saveContent(Content content){
        // 1、将发送内容保存在数据库中
        // contentService.saveOrUpdate(content)

        // 2、将发送的内容存入到redis的hash数据结构中
        Map<String, Object> contentMap = BeanMapUtils.beanToMap(content);
        redisTemplate.opsForHash().putAll(RedisConstants.PUSH_CONTENT_HASH,contentMap);
        // 设置key的过期时间
        redisTemplate.expire(RedisConstants.PUSH_CONTENT_HASH,30, TimeUnit.DAYS);
        return content;
    }

}
